//
//  CZEmoticonModel.swift
//  53.表情键盘新工程
//
//  Created by gzxzmac on 16/3/31.
//  Copyright © 2016年 gzxzmac. All rights reserved.
//

import UIKit

/// 表情模型
class CZEmoticonModel: NSObject, NSCoding {
    
    // 模型所在的文件夹
    var id: String

    // emoji 的16进制字符串
    var code: String? {
        didSet {
            // 转emoji
            let scanner = NSScanner(string: code!)
            
            var result: UInt32 = 0
            scanner.scanHexInt(&result)
            
            emoji = String(Character(UnicodeScalar(result)))
        }
    }
    
    var emoji: String?
    
    // 表情名称,用于网络传输
    var chs: String?
    
    // 表情图片名称,不能用于显示图片,需要全路径
    var png: String? {
        didSet {
            // 拼接全路径 = Emoticons.bundle/id/png
            fullPngPath = CZEmoticonPackageManager.bundlePath + "/" + id + "/" + png!
        }
    }
    
    var fullPngPath: String?
    
    /// 字典转模型
    init(id: String, dict: [String: String]) {
        self.id = id
        super.init()
        
        setValuesForKeysWithDictionary(dict)
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    
    override var description: String {
        return "\n \t 表情模型:code: \(code), chs: \(chs), png: \(png), fullPngPath: \(fullPngPath)"
    }
    
    /// 将emoticon表情模型转成带图片附件的属性文本
    func emotionModeToAttr(font: UIFont) -> NSAttributedString {
        // 图片->附件->属性文本
        let atachment = CZTextAttachment()
        atachment.image = UIImage(named: fullPngPath!)
        // 设置表情名称给附件
        atachment.chs = chs
        
        let wh = (font.lineHeight ?? 20)// * 0.8
        // bounds和frame正好相反,往下移是负数
        atachment.bounds = CGRect(x: 0, y: -3, width: wh, height: wh)
        
        // 转成可变的属性文本
        let attrM = NSMutableAttributedString(attributedString: NSAttributedString(attachment: atachment))
        
        // 给附件设置UIFont属性,不然后面的表情会很小
        attrM.addAttribute(NSFontAttributeName, value: font, range: NSRange(location: 0, length: 1))
        
        return attrM
    }
    
    // MARK: - 归档和解档
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(id, forKey: "id")
        aCoder.encodeObject(code, forKey: "code")
        aCoder.encodeObject(emoji, forKey: "emoji")
        aCoder.encodeObject(chs, forKey: "chs")
        aCoder.encodeObject(png, forKey: "png")
//        aCoder.encodeObject(fullPngPath, forKey: "fullPngPath")
    }
    
    required init?(coder aDecoder: NSCoder) {
        id = aDecoder.decodeObjectForKey("id") as? String ?? ""
        code = aDecoder.decodeObjectForKey("code") as? String
        emoji = aDecoder.decodeObjectForKey("emoji") as? String
        chs = aDecoder.decodeObjectForKey("chs") as? String
        png = aDecoder.decodeObjectForKey("png") as? String
//        fullPngPath = aDecoder.decodeObjectForKey("fullPngPath") as? String
        
        // 重新拼接全路径,因为在模拟器上面重新运行沙盒的路径会变化fullPngPath会失效
        if let p = png {
            fullPngPath = CZEmoticonPackageManager.bundlePath + "/" + id + "/" + p
        }
    }
}
